import https from 'https';  
import cheerio from 'cheerio';  
import SpiderOptions from "./interfaces/SpiderOptions";  
import { DatabaseService } from './DatabaseService';
  
interface Item {  
    id: string;  
    // ... 其他属性  
}  
  
export default class Spider03 {  
    options: SpiderOptions;  
  
    constructor(options: SpiderOptions = { url: '', method: 'get', headers:{}}) {  
        this.options = options;
        this.start();  
    }  
  
    async start(): Promise<void> {  
        try {  
            // 第一步：获取所有电影ID  
            const ids = await this.fetchIds();  
  
            // 第二步：根据每个ID获取电影详细信息  
            for (const id of ids) {  
                await this.fetchMovieDetails(id);  
            }  
  
            console.log('所有电影信息已获取');  
        } catch (error) {  
            console.error('获取电影信息时出错:', error);  
        }  
    }  
  
    private fetchIds(): Promise<string[]> {  
        return new Promise((resolve, reject) => {  
            let req = https.request(this.options.url, {  
                headers: this.options.headers,  
                method: this.options.method  
            }, (res) => {  
                let chunks: any[] = [];  
  
                res.on('data', (c) => chunks.push(c));  
  
                res.on('end', () => {  
                    let result = Buffer.concat(chunks).toString('utf-8');  
                    // console.log(result)
                    let data = JSON.parse(result).items;  
  
                    const ids: string[] = data.map((item: Item) => item.id);  
                    resolve(ids);  
                });  
  
                res.on('error', reject);  
            });  
  
            req.on('error', reject);  
            req.end();  
        });  
    }  
  
    private fetchMovieDetails(id: string): Promise<void> {  
        return new Promise((resolve, reject) => {  
            const url = 'https://movie.douban.com/subject/' + id + '/';  
            let req = https.request(url, {
                headers: this.options.headers,  
                method: this.options.method  
            }, (res) => {  
                let chunks: any[] = [];  
                 // 创建一个对象来存储键值对  
                const info:any = {};  
                res.on('data', (c) => chunks.push(c));  
  
                res.on('end', async () => {  
                    let result = Buffer.concat(chunks).toString('utf-8');  
                    const $ = cheerio.load(result);  
                    const databaseService = new DatabaseService();
                    // 提取电影标题等信息  
                    let textContent = '';  
                    $('#content h1 > span').each((index: number, element: any) => {  
                        textContent += $(element).text();  
                    });  
                    // console.log(`电影标题: ${textContent}`);  
                    info['标题'] = textContent;


                    // ... 可以继续提取其他信息 ...  
                    
                     // 信息部分
                    textContent ='';
                    $('#info').each((index:number, element:any)=>{
                        textContent = $(element).text();
                        // console.log(textContent)  
                        // console.log(`Span: ${index + 1} text: ${textContent}`);  
                    })
                    // 将字符串按行分割成数组  
                    // console.log(textContent)
                    const lines = textContent.trim().split('\n');  
                    // console.log(lines)
                    // // 创建一个对象来存储键值对  
                    // const info:any = {};  
                    
                    // 遍历每一行数据  
                    lines.forEach(line => {  
                    // 使用冒号分割字符串成两部分  
                    const [key, value] = line.split(':').map(str => str.trim());  
                    // 将键值对添加到对象中
                    if(key!=''&&key!=null&&value!=undefined){
                        info[key] = value; 
                    }  
                    });  
                    
                    // 输出处理后的对象  
                    // console.log(info);

                    //介绍部分
                    textContent ='';
                    $('.related-info .indent span[property="v:summary"]').each((index:number, element:any)=>{
                        textContent = $(element).text(); 
                        // console.log(`Span: ${index + 1} text: ${textContent}`);  
                    })
                    // console.log(textContent);
                    info['介绍'] = textContent.replace(/[\n\s]+/g, '');
                    const keys: string[] = ['title', 'director', 'writer','actors','genre','production_country','language','release_date','duration','alternative_names','imdb','description']; 
                    const value: string[]=[info['标题'],info['导演'],info['编剧'],info['主演'],info['类型'],info['制片国家/地区'],info['语言'],info['上映日期'],info['片长'],info['又名'],info['IMDb'],info['介绍']]
                    const map: Map<string, string> = new Map(keys.map((key, index) => [key, value[index]])); 
                    // console.log(map);
                    try {  
                            const result = await databaseService.insertData('movies', map); 
                            console.log('Data inserted successfully:', result);  
                        } catch (error) {  
                            console.error('Error inserting data:', error);  
                        }    
                    // console.log(info)
                        resolve();  
                    });  
    
                    res.on('error', reject);  
                });  
    
                req.on('error', reject);  
                req.end();  
        });  
    }  
}